/*
 *  PMFloat.h
 *  PezMonkey
 *
 *  Created by Spence DiNicolantonio on 11/4/09.
 *  Copyright 2009 Necrosoft. All rights reserved.
 *
 */

#import "PMCore.h"


#pragma mark Type Definition

/**
 * Defines PMFloat primitive data type. The Chipmunk source is altered to use 
 * floats unless the kDoublePrecision setting is turned on.
 */
typedef cpFloat PMFloat;


#pragma mark -
#pragma mark Static Inline Functions

/**
 * Returns the greater of two given values.
 */
static inline PMFloat PMFloatMax(PMFloat a, PMFloat b) {
	return (a > b) ? a : b;
}

/**
 * Returns the lesser of two given values.
 */
static inline PMFloat PMFloatMin(PMFloat a, PMFloat b) {
	return (a < b) ? a : b;
}

/**
 * Returns the next representable value after a, towards b.
 */
static inline PMFloat PMFloatNext(PMFloat a, PMFloat b) {
	if (PMDoublePrecision)
		return nextafter(a, b);
	else 
		return nextafterf(a, b);
}

/**
 * Clamps a given float to a given [min, max] range.
 */
static inline PMFloat PMFloatClamp(PMFloat f, PMFloat min, PMFloat max){
	return PMFloatMin(PMFloatMax(f, min), max);
}

/**
 * Generates a random PMFloat value in the range [0.0, 1.0]
 */
static inline PMFloat PMFloatRandom() {
	static BOOL seeded = NO;
	
	// seed random generator?
	if (!seeded) {
		srandom(time(NULL));
		seeded = YES;
	}
	
	return ((PMFloat)random() / (PMFloat)RAND_MAX);
}
